package me.zixing.java.chapter04.task02;

import me.zixing.java.chapter04.task02.dao.StudentDao;
import me.zixing.java.chapter04.task02.dao.StudentDaoWithArrayStoreImpl;
import me.zixing.java.chapter04.task02.entity.Student;

import java.util.Scanner;

/**
 * Created by bingh on 2017/10/12.
 */
public class Run {
    private static Scanner scanner = new Scanner(System.in);
    private static StudentDao dao = new StudentDaoWithArrayStoreImpl();
    private static  RunShutDownHook hook = new RunShutDownHook();
    public static void main(String[] args){

        Runtime.getRuntime().addShutdownHook(hook);
        System.out.println("学生管理系统");
        System.out.println("1.添加学生"+"\t\t"+"2.查询学生信息");
        while(true){
            System.out.println("请选择(输入0退出系统)：");
            int option = scanner.nextInt();
            switch (option){
                case 0:exit();return;
                case 1:addStudent();break;
                case 2:queryStudent();break;
                default:System.out.println("输入有误");
            }
        }

    }

    public static void exit(){
        Runtime.getRuntime().removeShutdownHook(hook);
        System.out.println("程序安全退出");
    }

    public static void addStudent(){

            if (dao.isFully()){
                System.out.println("储存已满,无法添加");
                return;
            }

            System.out.println("请输入学生信息:");
            System.out.println("请输入学生ID:");
            int studentId = scanner.nextInt();
            if(dao.hasStudentAlready(studentId)){
                System.out.println("该学号学生已存在");
                return;
            }
            System.out.println("请输入学生姓名:");
            String studentName = scanner.next();
            System.out.println("请输入学生语文成绩:");
            float chineseScore = scanner.nextFloat();
            System.out.println("请输入学生数学成绩:");
            float mathScore = scanner.nextFloat();
            System.out.println("请输入学生英语成绩:");
            float englishScore = scanner.nextFloat();
            Student student = new Student(studentId,studentName,chineseScore,mathScore,englishScore);
            dao.addStudent(student);

    }

    public static void queryStudent(){
        System.out.println("请输入学生ID:");
        int id = scanner.nextInt();
        Student student = dao.getStudentById(id);
        if(student == null){
            System.out.println("没有查到改学生任何信息!");
        }else{
            System.out.println(student);
        }
    }

}
